package com.app.utils.spring.datasource;

import com.app.base.BaseDao;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@MapperScan(value = "com.ys.*",markerInterface = BaseDao.class) //扫描mybatis dao
public class DatasourceConfig {


    @Bean("ysGoods")
    public DataSource ysGoods(Environment env) {
        HikariConfig config= DataSourceConfigUtil.setDataSourceEnvConfig("spring.datasource.ys_goods.","spring.datasource.ys_goods.hikari.",env);
        return new HikariDataSource(config);
    }
    @Bean("ysIm")
    @Primary
    public DataSource ysIm(Environment env) {
        HikariConfig config= DataSourceConfigUtil.setDataSourceEnvConfig("spring.datasource.ys_im.","spring.datasource.ys_im.hikari.",env);
        return new HikariDataSource(config);
    }
    @Bean("ysConfig")
    public DataSource ysConfig(Environment env) {
        HikariConfig config= DataSourceConfigUtil.setDataSourceEnvConfig("spring.datasource.ys_config.","spring.datasource.ys_config.hikari.",env);
        return new HikariDataSource(config);
    }
    @Bean("dynamicDataSource")
    public DataSource dynamicDataSource(@Qualifier("ysGoods") DataSource ysGoods,@Qualifier("ysIm") DataSource ysIm,@Qualifier("ysConfig") DataSource ysConfig) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("ysGoods", ysGoods);
        dataSourceMap.put("ysIm", ysIm);
        dataSourceMap.put("ysConfig", ysConfig);
        // 将 尚品好物 数据源作为默认指定的数据源
        dynamicDataSource.setDefaultDataSource(ysGoods);
        // 将 尚品好物 和 商品 数据源作为指定的数据源
        dynamicDataSource.setDataSources(dataSourceMap);
        return dynamicDataSource;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dynamicDataSource") DataSource all,Environment env) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 配置数据源，此处配置为关键配置，如果没有将 dynamicDataSource作为数据源则不能实现切换
        sessionFactory.setDataSource(all);
//        sessionFactory.setTypeAliasesPackage("com.louis.**.model");    // 扫描Model
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources(env.getProperty("mybatis.mapper-locations","classpath:mapping/**/*.xml")));    // 扫描映射文件
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(Boolean.parseBoolean(env.getProperty("mybatis.configuration.mapUnderscoreToCamelCase","true")));
        sessionFactory.setConfiguration(configuration);
        return sessionFactory;
    }

    @Bean
    public PlatformTransactionManager transactionManager(@Qualifier("dynamicDataSource") DataSource all) {
        // 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
        return new DataSourceTransactionManager(all);
    }
}
